﻿using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.ApproveCommand;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.ApproveCommandHandler
{
    public class UpdatLeaveModelCommandHandler : IRequestHandler<UpdatLeaveModelCommand, APIResult<string>>
    {
        private readonly IBaseRepository<LeaveModel> leaveRepos;

        public UpdatLeaveModelCommandHandler(IBaseRepository<LeaveModel> leaveRepos)
        {
            this.leaveRepos = leaveRepos;
        }
        /// <summary>
        /// `修改请假申请表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<string>> Handle(UpdatLeaveModelCommand request, CancellationToken cancellationToken)
        {
            APIResult<string> result = new APIResult<string>();
            //查询修改的model
            var obj = await leaveRepos.GetAll().Result.FirstOrDefaultAsync(x => x.LeaveId == request.LeaveId);
            obj.LeaveType = request.LeaveType;
            obj.LeavePeriod = request.LeavePeriod;
            obj.LeaveTime = request.LeaveTime;
            obj.LeaveEndTime = request.LeaveEndTime.AddHours(8);
            obj.LeaveReason = request.LeaveReason;  
            obj.AppStatus = request.AppStatus;  

            //修改
            int res = leaveRepos.Update(obj).Result;
            result.Code = APIEnum.修改成功;
            result.Msg = APIEnum.修改成功.ToString();
            result.data = res.ToString();
            return await Task.FromResult(result);
        }
    }
}
